package com.hl.card.credit.utill;

import com.alibaba.fastjson.JSONObject;
import java.util.*;

public class PinganPort {
    String versionNo="R10310";
    String code="m1804095435";
    String reserve="00000";
    String scc="235180089";
    String mt="0000000000000000000";
    String onlineSQFlag="N";
    String channel="WXHZF";
    String gurl="https://pacesapplystg.pingan.com/ca/applyIndex";
    String zgurl="https://c.pingan.com/ca/applypIndex";
    String purl="https://pacesapplystg.pingan.com/ca/applypIndex";
    String zpurl="https://c.pingan.com/ca/applypIndex";
    public void request(){
    JSONObject object=new JSONObject();
    object.put("versionNo",versionNo);
    object.put("scc",scc);
    object.put("onlineSQFlag",onlineSQFlag);
    object.put("channel",channel);
    object.put("partnerSeqId",getSeq());
    String  sc= SecrityTool.getEncrypt(object.toJSONString());
    String data=scc+mt+sc;
    String sign=getSign();
    Map<String,String > map=new HashMap<>();
    map.put("data",data);
    map.put("sign",sign);
    System.out.println(sign+"----------->"+data);
    try {
        String result=  HttpClientTool.doPost(purl,map);
        System.out.println(result);
    } catch (Exception e) {
        e.printStackTrace();
    }

}
    public void requestGET(){
        JSONObject object=new JSONObject();
        object.put("versionNo",versionNo);
        object.put("scc",scc);
        object.put("onlineSQFlag",onlineSQFlag);
        object.put("channel",channel);
        object.put("partnerSeqId",getSeq());
        String  sc= SecrityTool.getEncrypt(object.toJSONString());
        String data=scc+mt+sc;
        String sign=getSign();
        Map<String,String > map=new HashMap<>();
        map.put("data",data);
        map.put("sign",sign);
        System.out.println(sign+"----------->"+data);
        try {
            String result=  HttpClientTool.doGet(zgurl,map);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String requestPOST(){
        Map<String,String > object=new HashMap<>();
        object.put("versionNo","R10310");
        object.put("scc","235180089");
        object.put("onlineSQFlag","N");
        object.put("channel","WXHZF");
        System.out.println(getSeq());
        object.put("partnerSeqId",getSeq());
        String result = null;
        try {
             result=  HttpClientTool.doPost(zpurl,object);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    private String getSign() {
        Map<String, String> map = new TreeMap<>(Comparator.naturalOrder());
        map.put("versionNo","");
        map.put("scc",scc);
        map.put("onlineSQFlag",onlineSQFlag);
        map.put("channel",channel);
//        map.put("partnerSeqId",getSeq());
        Set<String> keySet = map.keySet();
        Iterator<String> iter = keySet.iterator();
        StringBuilder sb=new StringBuilder();
        while (iter.hasNext()) {
            String key = iter.next();
            System.out.println(key + ":"+ map.get(key));
            sb.append(key+"="+map.get(key)+"&");
        }
        String param= sb.subSequence(0,sb.length()-1).toString().toUpperCase();
        System.out.println(param);
        return  HMACSHA256.sha256_HMAC(param,HMACSHA256.secret_key);
    }

    public String getSeq() {
        //17位日期
        Calendar nowtime = new GregorianCalendar();
        String strDateTime = "" + String.format("%04d", nowtime.get(Calendar.YEAR)) + "" +
                String.format("%02d", nowtime.get(Calendar.MONTH)) + "" +
                String.format("%02d", nowtime.get(Calendar.DATE)) + "" +
                String.format("%02d", nowtime.get(Calendar.HOUR)) + "" +
                String.format("%02d", nowtime.get(Calendar.MINUTE)) + "" +
                String.format("%02d", nowtime.get(Calendar.SECOND)) + "" +
                String.format("%03d", nowtime.get(Calendar.MILLISECOND)) + "";
        //随机4位数
        int rand = (int) ((Math.random() * 9 + 1) * 1000);
        return code +reserve+strDateTime + rand;
    }

}
